package com.wlj.security.browser;

import com.wlj.security.core.authentication.AbstractChannelSecurityConfig;
import com.wlj.security.core.authentication.sms.SmsAuthenticationSecurityConfig;
import com.wlj.security.core.authorize.AuthorizeConfigManager;
import com.wlj.security.core.properties.WljSecurityProperties;
import com.wlj.security.core.validate.code.config.ValidateCodeSecurityConfig;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.social.security.SpringSocialConfigurer;

@Configuration
public class BrowserSecurityConfig extends AbstractChannelSecurityConfig {


    @Autowired
    private WljSecurityProperties wljSecurityProperties;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private SmsAuthenticationSecurityConfig smsAuthenticationSecurityConfig;

    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;

    /**
     * 配置SocialAuthenticationFilter生效
     */
    @Autowired
    private SpringSocialConfigurer wljSocialSecurityConfig;

    @Autowired
    private SessionInformationExpiredStrategy sessionInformationExpiredStrategy;

    @Autowired
    private InvalidSessionStrategy invalidSessionStrategy;

    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @Autowired
    private AuthorizeConfigManager authorizeConfigManager;

    /**
     * 配置"记住我"功能使用到的token存储服务
     * @return
     */
    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        return tokenRepository;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

           applyPasswordAuthenticationConfig(http);

           //配置formLogin:UsernamePasswordAuthenticationFilter生效
           //配置httpBasic:BasicAuthenticationFilter生效
           //maxSessionsPreventsLogin 配置达到最大会话数时是否阻止后面的登陆，默认 false(不阻止)
           http.apply(validateCodeSecurityConfig)
                   .and()
                .apply(smsAuthenticationSecurityConfig)
                   .and()
                .apply(wljSocialSecurityConfig)
                   .and()
                .rememberMe()
                    .tokenRepository(persistentTokenRepository())
                    .tokenValiditySeconds(wljSecurityProperties.getBrowser().getRememberMeSeconds())
                    .userDetailsService(userDetailsService)
                    .and()
                .sessionManagement()
                    .invalidSessionStrategy(invalidSessionStrategy)
                    .maximumSessions(wljSecurityProperties.getBrowser().getSession().getMaximumSessions())
                    .maxSessionsPreventsLogin(wljSecurityProperties.getBrowser().getSession().isMaxSessionsPreventsLogin())
                    .expiredSessionStrategy(sessionInformationExpiredStrategy)
                    .and()
                    .and()
                .logout()
                    .logoutUrl("/signOut")
                    .logoutSuccessHandler(logoutSuccessHandler)
                    .deleteCookies("JSESSIONID")    //指定要删除的浏览器cookie
                    .and()
                .headers().frameOptions().disable()
                    .and()
                .csrf().disable();

           authorizeConfigManager.config(http.authorizeRequests());
           //拥有admin角色才能访问/user，对应的权限是ROLE_ADMIN，注意区分大小写，其他只需登录认证
    }
}
